package com.cleaningcloud.erp.controller;

import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.erp.domain.ErpWareCheck;
import com.cleaningcloud.erp.domain.dto.ErpWareCheckDto;
import com.cleaningcloud.erp.domain.excelExport.WareCheckExcelExport;
import com.cleaningcloud.erp.domain.vo.ErpWareCheckDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpWareCheckVo;
import com.cleaningcloud.erp.service.IErpWareCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存盘点Controller
 *
 * @author cleaningcloud
 * &#064;date  2024-10-08
 */
@RestController
@RequestMapping("/ErpWareCheck")
public class ErpWareCheckController extends BaseController {
    @Autowired
    private IErpWareCheckService erpWareCheckService;

    /**
     * 查询库存盘点列表
     */
    @RequiresPermissions("erp:ErpWareCheck:list")
    @GetMapping("/list")
    public TableDataInfo list(ErpWareCheck erpWareCheck) {
        startPage();
        List<ErpWareCheckVo> list = erpWareCheckService.selectErpWareCheckList(erpWareCheck);
        return getDataTable(list);
    }

    /**
     * 导出库存盘点列表
     */
    @RequiresPermissions("erp:ErpWareCheck:export")
    @Log(title = "库存盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpWareCheck erpWareCheck) {
        List<ErpWareCheckVo> list = erpWareCheckService.selectErpWareCheckList(erpWareCheck);
        ExcelUtil<ErpWareCheckVo> util = new ExcelUtil<>(ErpWareCheckVo.class);
        util.exportExcel(response, list, "库存盘点数据");
    }

    /**
     * 导出库存盘点明细
     */
    @RequiresPermissions("erp:ErpWareCheck:export")
    @Log(title = "库存盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDetails/{wareCheckId}")
    public void exportDetails(HttpServletResponse response, @PathVariable Long wareCheckId) {
        ErpWareCheckVo wareCheckVo = erpWareCheckService.selectErpWareCheckByWareCheckId(wareCheckId);
        List<WareCheckExcelExport> list = new ArrayList<>();
        for (ErpWareCheckDetailsVo erpWareCheckDetailsVo : wareCheckVo.getDetailsList()) {
            WareCheckExcelExport wareCheckExcelExport = new WareCheckExcelExport();
            wareCheckExcelExport.setWareCheckOdd(wareCheckVo.getWareCheckOdd());
            wareCheckExcelExport.setWareCheckTitle(wareCheckVo.getWareCheckTitle());
            wareCheckExcelExport.setWareName(wareCheckVo.getWareName());
            wareCheckExcelExport.setWareCheckType(wareCheckVo.getWareCheckType());
            wareCheckExcelExport.setWareCheckTime(wareCheckVo.getWareCheckTime());
            wareCheckExcelExport.setWareCheckPersonName(wareCheckVo.getWareCheckPersonName());
            wareCheckExcelExport.setApproveStatus(wareCheckVo.getApproveStatus());
            wareCheckExcelExport.setApproveName(wareCheckVo.getApproveName());
            wareCheckExcelExport.setApproveTime(wareCheckVo.getApproveTime());
            wareCheckExcelExport.setCommodityNo(erpWareCheckDetailsVo.getCommodityNo());
            wareCheckExcelExport.setCommodityName1(erpWareCheckDetailsVo.getCommodityName1());
            wareCheckExcelExport.setCommodityType(erpWareCheckDetailsVo.getCommodityType());
            wareCheckExcelExport.setCommoditySpec(erpWareCheckDetailsVo.getCommoditySpec());
            wareCheckExcelExport.setUnitName1(erpWareCheckDetailsVo.getUnitName1());
            wareCheckExcelExport.setCostPrice(erpWareCheckDetailsVo.getCostPrice());
            wareCheckExcelExport.setWareCommodityNumber(erpWareCheckDetailsVo.getWareCommodityNumber());
            wareCheckExcelExport.setWareCheckNumber(erpWareCheckDetailsVo.getWareCheckNumber());
            wareCheckExcelExport.setQuantityNumber(erpWareCheckDetailsVo.getQuantityNumber());
            wareCheckExcelExport.setQuantityResult(erpWareCheckDetailsVo.getQuantityResult());
            wareCheckExcelExport.setQuantityPrice(erpWareCheckDetailsVo.getQuantityPrice());
            list.add(wareCheckExcelExport);
        }
        ExcelUtil<WareCheckExcelExport> util = new ExcelUtil<>(WareCheckExcelExport.class);
        util.exportExcel(response, list, "库存盘点数据");
    }

    @PostMapping("/importCommodityData")
    public AjaxResult importCommodityData(MultipartFile file) {
        try {
            ExcelUtil<ErpWareCheckDetailsVo> util = new ExcelUtil<ErpWareCheckDetailsVo>(ErpWareCheckDetailsVo.class);
            List<ErpWareCheckDetailsVo> commodityList = util.importExcel(file.getInputStream());
            return success(commodityList);
        }catch (Exception e) {
            return error("实盘数量不能为空！");
        }
    }

    /**
     * 获取库存盘点详细信息
     */
    @RequiresPermissions("erp:ErpWareCheck:query")
    @GetMapping(value = "/{wareCheckId}")
    public AjaxResult getInfo(@PathVariable("wareCheckId") Long wareCheckId) {
        return success(erpWareCheckService.selectErpWareCheckByWareCheckId(wareCheckId));
    }

    /**
     * 新增库存盘点
     */
    @RequiresPermissions("erp:ErpWareCheck:add")
    @Log(title = "库存盘点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpWareCheckDto erpWareCheck) {
        return toAjax(erpWareCheckService.insertErpWareCheck(erpWareCheck));
    }

    /**
     * 修改库存盘点
     */
    @RequiresPermissions("erp:ErpWareCheck:edit")
    @Log(title = "库存盘点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpWareCheckDto erpWareCheck) {
        return toAjax(erpWareCheckService.updateErpWareCheck(erpWareCheck));
    }

    /**
     * 审核调拨
     */
    @RequiresPermissions("erp:ErpWareCheck:audit")
    @Log(title = "审核盘点", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody ErpWareCheck erpWareCheck) {
        return toAjax(erpWareCheckService.auditErpWareCheck(erpWareCheck));
    }

    /**
     * 删除库存盘点
     */
    @RequiresPermissions("erp:ErpWareCheck:remove")
    @Log(title = "库存盘点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wareCheckIds}")
    public AjaxResult remove(@PathVariable Long[] wareCheckIds) {
        return toAjax(erpWareCheckService.deleteErpWareCheckByWareCheckIds(wareCheckIds));
    }
}
